package searchstring;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;


public class StringBuscaElsevierScopusBuilder extends AbstractStrategyStringBuscaBuilder{
	private static StringBuscaElsevierScopusBuilder instance;
	
	private static SearchCriteria searchCriteria;

	private StringBuscaElsevierScopusBuilder() {

	}

	/**
	 * This method returns the single instance of the class instance
	 * 
	 * @return {@link StringBuscaElsevierScopusBuilder}
	 */
	public static StringBuscaElsevierScopusBuilder getInstance() {
		if (StringBuscaElsevierScopusBuilder.instance == null) {
			StringBuscaElsevierScopusBuilder.instance = new StringBuscaElsevierScopusBuilder();
			
			//String searchMode = "";
			String searchMode = "TITLE-ABS-KEY";
			
			StringBuscaElsevierScopusBuilder.searchCriteria = new SearchCriteria();
			StringBuscaElsevierScopusBuilder.searchCriteria.setSearchMode(searchMode);
			

			StringBuscaElsevierScopusBuilder.searchCriteria.setMinYear("2002");
			
			String[] areas = {"COMP","ENGI", "SOCI", "MULT"};
			StringBuscaElsevierScopusBuilder.searchCriteria.setAreas(Arrays.asList(areas));

		}
		return StringBuscaElsevierScopusBuilder.instance;
	}

	public String getWebSource() {
		return "www.scopus.com";
	}
	
	public String getURL(String search_String){ 

		/*SearchCriteria searchCriteria = StringBuscaElsevierScopusBuilder.searchCriteria;
		search_String = searchCriteria.getSearchMode() + search_String;
		
		if(searchCriteria.getMinYear() != null){
			search_String+= " AND PUBYEAR > "+searchCriteria.getMinYear();
		}
		
		String areas_formatted = "";
		
		if(searchCriteria.getAreas().length > 0){
			areas_formatted = "&cluster=scosubjabbr";
			for(String area : searchCriteria.getAreas()){
				areas_formatted+= ("%2c%22"+area+"%22%2ct");
			}
		}
		
		
		String search_String_formatted = null;
		try {
			search_String_formatted = java.net.URLEncoder.encode(search_String, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		
		return "http://www.scopus.com/results/results.url?sort=plf-f&src=s&nlo=&nlr=&nls=&sot=a&sdt=cl" +
				areas_formatted+"&sl=319" +
				"&s="+search_String_formatted+"&zone=leftSideBar";*/
		
		return null;
	}

	public Map<String, String> getMapQuestaoPesquisaStringBuilder() {
		Map<String, String> mapQuestaoPesquisaStringBuilder = new LinkedHashMap<String, String>();

		for (QuestaoPesquisa questaoPesquisa : StringBuscaLoader.getInstance().getQuestoesPesquisa()) {

			String stringBusca = "";
			String separador = "";
			for (TokenStringBusca tokenStringBusca : questaoPesquisa.getTokensStringBusca()) {
				stringBusca+= separador + tokenStringBusca.getStringBuscaSinonimos(false);
				separador = " AND ";
			}

			SearchCriteria searchCriteria = StringBuscaElsevierScopusBuilder.searchCriteria;
			
			String stringBuscaCriteria = searchCriteria.getSearchMode()+"("+stringBusca+")";
			
			if(searchCriteria.getMinYear() != null){
				stringBuscaCriteria+= " AND PUBYEAR > "+searchCriteria.getMinYear();
			}
			
			String areas_formatted = "";
			
			if(searchCriteria.getAreas().isEmpty() == false){
				areas_formatted = " AND (";
				for(int i=0; i < searchCriteria.getAreas().size(); i++){
					areas_formatted+= ("LIMIT-TO(SUBJAREA,\""+searchCriteria.getAreas().get(i)+"\")");
					if(i != (searchCriteria.getAreas().size() -1)){
						areas_formatted+= " OR ";
					}
				}
				areas_formatted+= ")";
			}
			
			stringBuscaCriteria+= areas_formatted;
			
						
			mapQuestaoPesquisaStringBuilder.put(questaoPesquisa.getName(), stringBuscaCriteria);
		}

		return mapQuestaoPesquisaStringBuilder;
	}

}
